import Vue from 'vue'
import Router from 'vue-router'


import home from '../views/home.vue'  //首页组件
import table from '../views/table.vue'
import userTable from '../views/userTable.vue'
import login from '../views/login'

Vue.use(Router)


const router = new Router({
  routes: [
    {
      path: '/home',
      name: 'Home',
      component: home
    },
    {
      path:'/table',
      name:'Table',
      component:table
    },
    {
      path:'/userTable',
      name:'userTable',
      component:userTable
    },
    {
      path:'/login',
      name:'login',
      component:login
    }

  ]
})
export default router
//全局守卫
router.beforeEach((to, from, next) => {
 let token = localStorage.getItem('mytoken')
 //如果已登录，放行
 if(token){
   next()
 }else{
   //如果没有登录，访问非登录页面，则跳转到登录页面
   if(to.path !=='/login'){
     next({
       path:'/login'
     })
   }else{
     //如果没有登录，但访问的是登录页面，直接进入
     next()
   }
 }
})